<?php
class UsersModel extends BaseModel
{
    //自动验证
    protected $_validate = array(
        array('name', 'require', '名称必须！', self::MUST_VALIDATE),
        array('email', 'require', '邮件地址必须！', self::MUST_VALIDATE),
        array('pwd', 'require', '密码必须！', self::VALUE_VALIDATE),
        array('repwd', 'require', '确认密码必须！', self::VALUE_VALIDATE),
        array('birthday', 'require', '生日必须！', self::VALUE_VALIDATE),
        array('email', 'email', '邮箱格式错误！', self::VALUE_VALIDATE),
        array('repwd', 'pwd', '确认密码不一致！', self::EXISTS_VALIDATE, 'confirm'),
        array('email', '', '邮箱地址已经存在！', 0, 'unique', self::EXISTS_VALIDATE),
    );
    //自动完成
    protected $_auto = array(
        array('pwd', 'md5', self::MODEL_BOTH, 'function'),
        array('reg_time', 'time', self::MODEL_INSERT, 'function'),
        array('reg_ip', 'get_client_ip', self::MODEL_INSERT, 'function')
    );

    /**
     * 检测用户手机号码认证情况
     * @param Int $uid 用户标识
     * @return array
     * @author ice <iceinto@mallog.com.cn>
     */
    public function checkUsePhone($uid)
    {
        $data = array();
        //1.检查用户状态是否是 2
        $user = $this->find($uid);
        if ($user['status'] == 2) {
            //用户是绑定成功状态
            return $this->mReturn('用户信息已经在绑定状态！', true);
        }
        return $this->mReturn(null, false);
    }

    /**
     * 检测电话号码可用性
     * @param int $phone 手机号码
     * @return array 查询结果
     * @author ice <iceinto@mallog.com.cn>
     */
    public function checkPhoneIsUse($phone)
    {
        $map = array();
        $map['phone'] = $phone;
        return $this->where($map)->find();
    }

    /**
     * 用户状态修改和电话号码
     * @param int $uid
     * @param int $status
     * @param int $phone
     * @author ice <iceinto@mallog.com.cn>
     */
    public function updateUserStatus($uid, $status = 2, $phone = null)
    {
        $data = array();
        $data['id'] = $uid;
        $data['status'] = $status;
        if ($phone) {
            $data['phone'] = $phone;
        }
        return $this->save($data);
    }

    /**
     * 根据Email地址获取用户信息
     * @param string $email
     * @param string $fields
     * @author ice <iceinto@mallog.com.cn>
     */
    public function getOneByEmail($email, $fields = 'id,name,email')
    {
        $map = array();
        $map['email'] = $email;
        return $this->getByWhere($map, $fields);
    }

    /**
     * 获取昵称数量
     * @param $name
     * @return mixed
     * @author ice <iceinto@mallog.com.cn>
     */
    public function getCountByName($name)
    {
        $map = array();
        $map['name'] = $name;
        return $this->where($map)->count();
    }
}